package com.chookss.tiku

import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.tiku.entity.Subject
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.entity.ReciteEntity
import com.chookss.tiku.response.GetSameDayPracticeNum
import com.chookss.tiku.response.PracticeSubjectList
import com.chookss.tiku.response.PracticeSubmit
import com.chookss.tiku.response.ReciteSubmit
import com.chookss.tiku.util.TiKuUtils
import com.chookss.tools.Utils
import com.chookss.tools.httpForm
import com.johnrambo.ktea.net.http.Method

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2020/12/28
 * Note: 模拟试卷数据类
 */
class PracticeTestViewModel() : BasePracticeTestViewModel() {

    //    catalogCodes	false	String	目录编码，多个用逗号分隔(专项练习模式下此参数必传)
//    pageNum	true	Integer	页码
//    pageSize	true	Integer	页面加载量
    private val pageSize = 10
    lateinit var catalogCodes: String
    lateinit var practiceType: String
    lateinit var recordUid: String
    lateinit var memoryLvl: String
    lateinit var subjectTypeCode: String

    //题目列表
    val subjectPageMap = mutableMapOf<Int, List<Subject>>()

    private fun createSubjectResult(
        subjectList: List<Subject>,
        listIndex: Int
    ): LiveResult<Subject> {
        if (subjectList.size > listIndex) {
            return LiveResult(subjectList[listIndex])
        } else {//没有结果,结束了
            return LiveResult(null)
        }
    }

    //下载题目列表
    override fun getSubject(index: Int): LiveData<LiveResult<Subject>> {
        var liveResult = MutableLiveData<LiveResult<Subject>>()
        var pageNum = index / pageSize
        var listIndex = index % pageSize
        if (index != 0) {
            var subjectList = subjectPageMap[pageNum]
            if (subjectList != null) {
                liveResult.postValue(createSubjectResult(subjectList, listIndex))
                return liveResult
            }
        }else{
            subjectPageMap.clear()
        }
        val params2: MutableMap<String, Any?> = HashMap()
        if (!TextUtils.isEmpty(catalogCodes)) {
            params2["catalogCodes"] = catalogCodes
        }
        if (!Utils.isNull(memoryLvl)) {
            params2["memoryLvl"] = memoryLvl
        }
        if (!Utils.isNull(subjectTypeCode)) {
            params2["subjectTypeCode"] = subjectTypeCode
        }
        params2["practiceType"] = practiceType
        params2["pageNum"] = (pageNum + 1).toString()
        params2["pageSize"] = pageSize.toString()
        jobs + httpForm<PracticeSubjectList.Response> {
            url = "api/practice/getSubjectByCatalog"
            params = params2
            onSuccess = {
                var subjectList = it.data ?: emptyList()
                subjectPageMap[pageNum] = subjectList
                liveResult.postValue(createSubjectResult(subjectList, listIndex))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }


    fun getSameDayPracticeNum(): LiveData<LiveResult<GetSameDayPracticeNum.Result>> {
        var liveResult = MutableLiveData<LiveResult<GetSameDayPracticeNum.Result>>()

        jobs + httpForm<GetSameDayPracticeNum.Response> {
            url = "api/practice/getSameDayPracticeNum"
            params = mutableMapOf("practiceType" to practiceType)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //提交答案
    override fun submitAnswer(
        subject: Subject,
        answers: List<String>,
        time: Long
    ): LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()
        var cache = subjectAnswerCache[subject.subjectCode]
        if (cache == null) {
            liveResult.postValue(LiveResult(Error("选项列表为空")))
            return liveResult
        }
        if (answers.isEmpty()) {
            liveResult.postValue(LiveResult(Error("请先答题,再提交")))
            return liveResult
        }

//        catalogCode	true	String	题目所属的目录编码，多个逗号分隔
//        practiceType	true	String	练习类型 1智能题库 2 专项练习
//        subjectTypeCode	true	String	题目类型，题目信息中可获取该字段
//        subjectCode	true	String	题目编码
//        answerOption	true	String	答案选项
//        optionLvlNo	true	String	选项序列号
//        timeLength	true	String	答题所用时长，单位分钟
//        isRight	true	String	是否作对 0否 1是


        val params2: MutableMap<String, Any?> = HashMap()
        params2["catalogCode"] = subject.catalogCode
        params2["practiceType"] = practiceType
        params2["subjectTypeCode"] = subject.subjectTypeCode
        params2["subjectCode"] = subject.subjectCode
        if(subject.subjectTypeCode.equals("1004")){
            params2["optionLvlNo"] = answers.joinToString("@#^")
        }else{
            params2["optionLvlNo"] = answers.joinToString(",")
        }

        params2["timeLength"] = (time / 1000).toString()
        params2["isRight"] = if (TiKuUtils.myAnswerIsRight(cache, answers)) "1" else "0"

        jobs + httpForm<PracticeSubmit.Response> {
            url = "api/practice/submitSubject"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data.isRight))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //背题
    fun toRecite(
        subject: Subject,
        time: Int,
        isFirst: Boolean
    ): LiveData<LiveResult<ReciteEntity>> {
        var liveResult = MutableLiveData<LiveResult<ReciteEntity>>()
        val params2: MutableMap<String, Any?> = HashMap()
        if (isFirst) {
            params2["subjectCode"] = ""
            params2["timeLength"] = ""
        } else {
            params2["subjectCode"] = subject.subjectCode
            params2["timeLength"] = time.toString()
        }
        jobs + httpForm<ReciteSubmit.Response> {
            url = "api/memoryCurve/toRecite"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

}
